﻿using System;
using Pathfinding;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace Yoozoo.Gameplay.City.AI
{
    public class CityAmbientNPCManager:MonoBehaviour
    {
        public static void UpdateAStarColider(Collider collider)
        {
            if (collider != null && AstarPath.active != null)
            {
                //AstarPath.active.UpdateGraphs(collider.bounds);
            }
        }
        public static void LoadMapSetting(byte[] bytes, Transform root)
        {
            //AstarPath.active.data.DeserializeGraphs(bytes);
            //AstarPathUtils.DeserializeGraphsAdditive(bytes);
              
            //PointGraph graph = AstarPath.active.data.pointGraph;
            //graph.root = root;
            //graph.searchTag = "CityRoomPoint";
            //AstarPath.active.Scan(new NavGraph[] {AstarPath.active.data.gridGraph, graph});
            //AstarPath.active.logPathResults = PathLog.OnlyErrors;
        }

        public static void RefreshPointMap()
        {
            if (AstarPath.active)
            {
                PointGraph graph = AstarPath.active.data.pointGraph;
                if (graph != null)
                {
                    AstarPath.active.Scan(graph);
                }
            }
            
        }

        private static Action scanGridCallback;
        public static void ScanGridMap(Action callBack)
        {
            scanGridCallback = callBack;
            if (AstarPath.active)
            {
                GridGraph gridGraph = AstarPath.active.data.gridGraph;
                if (gridGraph != null)
                {
                    AstarPath.OnGraphPostScan += OnGraphPostScan;
                    AstarPath.active.Scan(new NavGraph[]{gridGraph});
                }
            }
        }

        private static void OnGraphPostScan(NavGraph graph)
        {
            if (graph == AstarPath.active.data.gridGraph)
            {
                scanGridCallback?.Invoke();
            }
        }

        public static void RefreshMap()
        {
            if (AstarPath.active)
            {
                GridGraph gridGraph = AstarPath.active.data.gridGraph;
                PointGraph pointGraph = AstarPath.active.data.pointGraph;
                if (gridGraph != null && pointGraph != null)
                {
                    AstarPath.active.Scan(new NavGraph[]{gridGraph, pointGraph});
                }
            }
        }

    

    }
}

